import Cookies from 'js-cookie'
import {CACHE_START_STRING} from "@/constants/data";
import {message} from "ant-design-vue";
import {defineAsyncComponent} from "vue";

export const localCache = {
    setCache(key: string, value: unknown) {
        localStorage.setItem(CACHE_START_STRING + key, JSON.stringify(value))
    },
    getCache(key: string) {
        const result = localStorage.getItem(CACHE_START_STRING + key)
        if (result !== null) {
            return JSON.parse(result);
        } else {
            return ""
        }
    },
    clear() {
        localStorage.clear()
    }
}
export const cookieCache = {
    setCache(key: string, value: unknown, expires?: number) {
        if (expires) {
            Cookies.set(key, value, {
                expires
            })
        } else {
            Cookies.set(key, value)
        }
    },
    getCache(key: string) {
        return Cookies.get(key);
    }
}

export const checkUserPermission = (loginUserPermission: number, recordUserPermission: number) => {
    return new Promise((resolve, reject) => {
        if (loginUserPermission > recordUserPermission) {
            resolve(true)
        } else {
            reject(false)
        }
    })
}

// 获取异步组件
export const getAsyncComponent = (path: string) => {
    return defineAsyncComponent(() => import(/* @vite-ignore */path))
}